/**
 * 
 */
package com.jzcodecheck.ui.core.processor.parse.file;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JTextArea;

import com.autoscript.ui.helper.FileCtrlUtils;
import com.autoscript.ui.helper.StringHelper;
import com.jzcodecheck.ui.core.UIConstants;
import com.jzcodecheck.ui.core.processor.file.IAutoScriptFile;
import com.jzcodecheck.ui.core.processor.file.TextFile;
import com.jzcodecheck.ui.core.processor.parse.function.CreateTextFileParse;
import com.jzcodecheck.ui.helper.UIPropertyHelper;
import com.jzcodecheck.ui.model.projecttree.TemplateModel;

/**
 * 文件解析器实现类
 * 作者:龙色波
 * 日期:2013-10-15
 */
public class FileParse implements IFileParse {
	private Map<String,IAutoScriptFile> autoScriptFileMap = new HashMap<String,IAutoScriptFile>();
	private JTextArea outputTextArea;
	public FileParse(JTextArea outputTextArea) {
		this.outputTextArea = outputTextArea; 
	}
	/* (non-Javadoc)
	 * @see com.autoscript.ui.core.processor.parse.file.IFileParse#parse(java.lang.String, com.autoscript.ui.model.projecttree.TemplateModel)
	 */
	@Override
	public void parse(String fileName, TemplateModel templateModel)
			throws Exception {
		String keys[]={UIConstants.CREATE_TEXT_FILE_PREFIX,UIConstants.FUNCTION_SUFFIX,UIConstants.CLOSE_FILE};
		List lines = FileCtrlUtils.readLines(new File(fileName), UIConstants.UTF_8);
		int startpos=0;
		int startfunpos;
		int endfunpos;
		String line;
		boolean isWriting = true;
		String newFileName="";
		List writerLines = new ArrayList();
		CreateTextFileParse funParse = new CreateTextFileParse();
		
		for(int i=0;i<lines.size();i++){
			line = (String)lines.get(i);
			if(StringHelper.findStrNum(line, keys[0])>0 && StringHelper.findStrNum(line, keys[1])>0){
				startpos = StringHelper.findStrIgnoreCase(line, keys[0], 0);
				if(startpos==-1){
					System.out.println("行内容:"+line);
					throw new Exception(keys[0]+"在同一行缺少"+keys[1]);
				}else{
					//查找函数开始和结束，开始和结束必须在一行
					startfunpos = startpos+keys[0].length();
					endfunpos = StringHelper.findStrIgnoreCase(line, keys[1], startfunpos);
					if(endfunpos==-1)
						throw new Exception(keys[0]+"在同一行缺少"+keys[1]);
					//解释函数参数					
					List<Object> parameters = funParse.parseFunctionParmeter(line.substring(startfunpos, endfunpos));
					if(parameters!=null && parameters.size()>0){
						newFileName = (String)parameters.get(0);
						isWriting =true;
					}
				}
			//判断是否遇到结束函数
			}else if(StringHelper.findStrNum(line, keys[2])>0 && StringHelper.findStrNum(line, keys[1])>0){
				isWriting = false;
				FileCtrlUtils.writeLines(new File(newFileName), UIConstants.UTF_8, writerLines, "\r\n");
				addOutputMessage("生成文件:"+newFileName);
				writerLines.clear();
			}else{
				if(isWriting){
					writerLines.add(line);
				}
			}
		}
	}
	/**
	 * 忽略行
	 * @param fileContent
	 * @param startconentpos
	 * @return
	 */
	private int ignoreLineChar(String fileContent, int startconentpos) {
		if(fileContent.charAt(startconentpos)=='\r'||fileContent.charAt(startconentpos)=='\n'){
			return startconentpos+1;
		}
		return startconentpos;
	}
	/**
	 * 构造
	 * @param funname  函数名
	 * @return
	 * @throws Exception 
	 */
	private IAutoScriptFile makeAutoScriptFile(String funname) throws Exception {
		IAutoScriptFile autoScriptFile = autoScriptFileMap.get(funname);
		if(autoScriptFile==null){
			if(UIConstants.CREATE_TEXT_FILE_PREFIX.equalsIgnoreCase(funname)){
				autoScriptFile = new TextFile();
				autoScriptFileMap.put(funname, autoScriptFile);
			}else
				throw new Exception(UIPropertyHelper.getString("exception.invalidateCreateFileFun",funname));
		}
		return autoScriptFile;
	}
	public void addOutputMessage(String msg){
		if(outputTextArea!=null){
		  if(msg.endsWith("\r\n")){
		   outputTextArea.setText(outputTextArea.getText()+msg);
		  }else{
			  outputTextArea.setText(outputTextArea.getText()+msg+"\r\n");  
		  }
		}
	}
}
